package net.cyan.nest;

import javax.naming.*;
import java.lang.reflect.Type;
import java.util.Hashtable;

/**
 * 
 * @author camel
 * @date 2008-11-23
 */
public class JndiBeanFactory extends AbstractBeanFactory
{
    private String name;

    private Hashtable<String, String> env;

    private Context context;

    public JndiBeanFactory()
    {
    }

    public JndiBeanFactory(String name, Hashtable<String, String> env)
    {
        this.name = name;
        this.env = env;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public Hashtable<String, String> getEnv()
    {
        return env;
    }

    public void setEnv(Hashtable<String, String> env)
    {
        this.env = env;
    }

    public synchronized Context getContext() throws Exception
    {
        if (context == null)
            context = new InitialContext(env);
        return context;
    }

    public Object newInstance(Type type, BeanContainer container, String name) throws Exception
    {
        return getContext().lookup(name);
    }

    public Class<?> getInstanceType(Class<?> c) throws Exception
    {
        return Object.class;
    }
}
